package com.enjoyor.soa.traffic.server.tsms.handler;

import com.enjoyor.soa.traffic.api.dto.gps.PositionDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RouteInfoDto;
import com.enjoyor.soa.traffic.api.service.dubbo.gps.PositionService;
import com.enjoyor.soa.traffic.api.service.dubbo.idcs.IDCSCallbackListener;
import com.enjoyor.soa.traffic.api.service.dubbo.tsms.IDubboRouteInfoService;
import com.enjoyor.soa.traffic.core.tsms.domain.*;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.tsms.service.ISchemeInfoService;
import com.enjoyor.soa.traffic.core.tsms.service.ITaskExecuteRecordService;
import com.enjoyor.soa.traffic.core.tsms.service.ITaskInfoService;
import com.enjoyor.soa.traffic.core.tsms.util.SpringContextUtil;
import com.enjoyor.soa.traffic.server.tsms.common.RehearseGlobal;
import com.enjoyor.soa.traffic.server.tsms.dubbo.impl.DubboPositionServiceImpl;
import com.enjoyor.soa.traffic.server.tsms.dubbo.impl.DubboServerPushServiceImpl;
import com.enjoyor.soa.traffic.server.tsms.dubbo.invoke.DubboVehicleCallbackServiceInvoke;
import com.enjoyor.soa.traffic.server.tsms.helper.EntityChangeHelper;
import com.enjoyor.soa.traffic.server.tsms.helper.VehiclePassListenerImpl;
import com.enjoyor.soa.traffic.server.tsms.helper.PositionListenerImpl;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import org.apache.ibatis.jdbc.Null;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 接收数据，数据清理
 */
public class InitializeHandler extends BasicMsgHandler<Map<String,String>> {
	private InitializeHandler() {
		super(1000,"实例化");
	}

	private static InitializeHandler single;

	public static InitializeHandler getInstance() {
		if (single == null) {
			synchronized (InitializeHandler.class) {
				if (single == null) {
					single = new InitializeHandler();
					single.setMultiProcess(2, 20, 2);
				}
			}
		}
		return single;
	}


	@Override
	protected boolean task(int taskTimes){
        return true;
    }

	private ISchemeInfoService iSchemeInfoService;
	public ISchemeInfoService getSchemeInfoService(){
		if(iSchemeInfoService==null){
			iSchemeInfoService = (ISchemeInfoService) SpringContextUtil.getBean(ISchemeInfoService.class);
		}
		return iSchemeInfoService;
	}

	private ITaskExecuteRecordService iTaskExecuteRecordService;
	public ITaskExecuteRecordService getTaskExecuteRecordService(){
		if(iTaskExecuteRecordService==null){
			iTaskExecuteRecordService = (ITaskExecuteRecordService) SpringContextUtil.getBean(ITaskExecuteRecordService.class);
		}
		return iTaskExecuteRecordService;
	}

	private DubboServerPushServiceImpl dubboServerPushServiceImpl;
	public DubboServerPushServiceImpl getServerPushServiceImpl(){
		if(dubboServerPushServiceImpl==null){
			dubboServerPushServiceImpl = (DubboServerPushServiceImpl) SpringContextUtil.getBean(DubboServerPushServiceImpl.class);
		}
		return dubboServerPushServiceImpl;
	}
	private DubboPositionServiceImpl positionService;
	public DubboPositionServiceImpl getPositionService(){
		if(positionService==null){
			positionService = (DubboPositionServiceImpl) SpringContextUtil.getBean(DubboPositionServiceImpl.class);
		}
		return positionService;
	}
	private IDubboRouteInfoService iDubboRouteInfoService;
	public IDubboRouteInfoService getDubboRouteInfoService(){
		if(iDubboRouteInfoService==null){
			iDubboRouteInfoService = (IDubboRouteInfoService) SpringContextUtil.getBean(IDubboRouteInfoService.class);
		}
		return iDubboRouteInfoService;
	}
	private ITaskInfoService iTaskInfoService;
	public ITaskInfoService getTaskInfoService(){
		if(iTaskInfoService==null){
			iTaskInfoService = (ITaskInfoService) SpringContextUtil.getBean(ITaskInfoService.class);
		}
		return iTaskInfoService;
	}
	private DubboVehicleCallbackServiceInvoke vehicleCallbackService;
	private String taskId;
	public DubboVehicleCallbackServiceInvoke getVehicleCallbackService(){
		if(vehicleCallbackService==null){
			vehicleCallbackService = (DubboVehicleCallbackServiceInvoke) SpringContextUtil.getBean(DubboVehicleCallbackServiceInvoke.class);
		}
		return vehicleCallbackService;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected boolean process(Map<String,String> msg) {
		String routeId=msg.get("routeId");
		String executeTaskId=msg.get("executeTaskId");
		String state=msg.get("state");
		String positionDeviceId=msg.get("positionDeviceId");
		String personName=msg.get("personName");
		if ("position".equals(state)) {
			//只是监测定位数据
			if(RehearseGlobal.ActionDataPool.containsKey(executeTaskId)) {
				RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);//暂停
				if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid")!=null){//删除定位订阅
					this.getPositionService().removeListener(Long.parseLong(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid").toString()));
				}
				if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listenerIdPass")!=null){//删除卡口过车数据订阅
					this.getVehicleCallbackService().removeVehiclePassListener(executeTaskId);
				}
				RehearseGlobal.ActionDataPool.remove(executeTaskId);//移除缓存信息
			}
			RehearseGlobal.ActionDataPool.put(executeTaskId,new ConcurrentHashMap<>());
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("routeId",routeId);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("executeTaskId",executeTaskId);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("position",true);//开启监测定位
			//初始化化数据
			datainit(executeTaskId,positionDeviceId);
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("positionDeviceId")!=null){
				List<String> devices = new ArrayList<String>();
				devices.add(RehearseGlobal.ActionDataPool.get(executeTaskId).get("positionDeviceId").toString());
				ResultPojo listerPojo = this.getPositionService().addListener(devices, PositionListenerImpl.getInstance());
				String listerid = listerPojo.getResultList().toString();
				RehearseGlobal.ActionDataPool.get(executeTaskId).put("listerid",listerid);
			}
			//修改执行任务状态为待执行
			String executeStatusId = EnumDicPublic.EXECUTE_WAIT.getKey();
			String executeStatusName = EnumDicPublic.EXECUTE_WAIT.getValue();
			this.getSchemeInfoService().editExecuteTaskStatus(executeTaskId, executeStatusId, executeStatusName, personName);
			String recordContent = RehearseGlobal.ActionDataPool.get(executeTaskId).get("taskName")+":开始监测定位数据!";
			//记录开始监测信息
			TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
			taskExecuteRecord.setExecuteTaskId(executeTaskId);
			taskExecuteRecord.setIsWarn("0");
			taskExecuteRecord.setRecordTypeId(EnumDicPublic.INFO_START_MONITOR.getKey());
			taskExecuteRecord.setRecordTypeName(EnumDicPublic.INFO_START_MONITOR.getValue());
			taskExecuteRecord.setRecordContent(recordContent);
			this.getTaskExecuteRecordService().insertRecord(taskExecuteRecord, personName);
			//推送开始监测消息
			com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
			Map<String, Object> startMap = new HashMap<String, Object>();
			startMap.put("type", "position");
			startMap.put("recordTime", TimeHelper.dateToLongString());
			startMap.put("content", recordContent);
			jsonObject2.put(routeId, startMap);
			this.getServerPushServiceImpl().pushMessage("" + executeTaskId, jsonObject2);
			ClashPrognosisHandler.getInstance().handle(executeTaskId);//预测线路冲突情况
		} else if ("start".equals(state)) {
			if(RehearseGlobal.ActionDataPool.containsKey(executeTaskId)) {
				RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);//暂停
				if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid")!=null){//删除定位订阅
					this.getPositionService().removeListener(Long.parseLong(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid").toString()));
				}
				if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listenerIdPass")!=null){//删除卡口过车数据订阅
					this.getVehicleCallbackService().removeVehiclePassListener(executeTaskId);
				}
				RehearseGlobal.ActionDataPool.remove(executeTaskId);//移除缓存信息
			}
			//开始执行
			RehearseGlobal.ActionDataPool.put(executeTaskId,new ConcurrentHashMap<>());
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("routeId",routeId);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("executeTaskId",executeTaskId);

			Map<String, Object> lastGeo = new HashMap<String, Object>();//上一次有效定位数据
			Map<String, Object> lastDeviateGeo = new HashMap<String, Object>();//偏离报警的上一次有效定位数据
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("lastGeo",lastGeo);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("lastDeviateGeo",lastDeviateGeo);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",false);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("positionWranNum",0);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("isDeviateCoord",false);
			//初始化化数据
			datainit(executeTaskId,positionDeviceId);
			showfirstsigne(executeTaskId);
			showfirstVideo(executeTaskId);
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("positionDeviceId")!=null){
				List<String> devices = new ArrayList<String>();
				devices.add(RehearseGlobal.ActionDataPool.get(executeTaskId).get("positionDeviceId").toString());
				String listerid = this.getPositionService().addListener(devices, PositionListenerImpl.getInstance())
						.getResultList().toString();
				RehearseGlobal.ActionDataPool.get(executeTaskId).put("listerid",listerid);
			}
			//修改执行任务状态为执行中
			String executeStatusId = EnumDicPublic.EXECUTE_RUNNING.getKey();
			String executeStatusName = EnumDicPublic.EXECUTE_RUNNING.getValue();
			this.getSchemeInfoService().editExecuteTaskStatus(executeTaskId, executeStatusId, executeStatusName, personName);
			
			String recordContent = RehearseGlobal.ActionDataPool.get(executeTaskId).get("taskName")+":开始执行!";
			//记录开始执行信息
			TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
			taskExecuteRecord.setExecuteTaskId(executeTaskId);
			taskExecuteRecord.setIsWarn("0");
			taskExecuteRecord.setRecordTypeId(EnumDicPublic.INFO_START_EXECUTE.getKey());
			taskExecuteRecord.setRecordTypeName(EnumDicPublic.INFO_START_EXECUTE.getValue());
			taskExecuteRecord.setRecordContent(recordContent);
			this.getTaskExecuteRecordService().insertRecord(taskExecuteRecord, personName);
			//推送开始执行消息
			com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
			Map<String, Object> startMap = new HashMap<String, Object>();
			startMap.put("type", "begin");
			startMap.put("recordTime", TimeHelper.dateToLongString());
			startMap.put("content", recordContent);
			jsonObject2.put(routeId, startMap);
			this.getServerPushServiceImpl().pushMessage("" + executeTaskId, jsonObject2);
			ClashPrognosisHandler.getInstance().handle(executeTaskId);//预测线路冲突情况
		}else if ("stop".equals(state) && RehearseGlobal.ActionDataPool.containsKey(executeTaskId)) {
			//执行结束
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid")!=null){//删除定位订阅
				this.getPositionService().removeListener(Long.parseLong(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid").toString()));
			}
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listenerIdPass")!=null){//删除卡口过车数据订阅
				this.getVehicleCallbackService().removeVehiclePassListener(executeTaskId);
			}
			//清理锁定的设备
			for (Map.Entry<String, Map<String, Object>> m : RehearseGlobal.VideoLockPool.entrySet()) {
        		Map<String, Object> videoMap = m.getValue();
        		if(executeTaskId.equals((String)videoMap.get("executeTaskId"))) {
        			RehearseGlobal.VideoLockPool.remove(m.getKey());
        		}
        	}
			for (Map.Entry<String, Map<String, Object>> m : RehearseGlobal.SignalLockPool.entrySet()) {
        		Map<String, Object> signalMap = m.getValue();
        		if(executeTaskId.equals((String)signalMap.get("executeTaskId"))) {
        			RehearseGlobal.SignalLockPool.remove(m.getKey());
        		}
        	}
			//修改执行任务状态为执行完成
			String executeStatusId = EnumDicPublic.EXECUTE_FINISH.getKey();
			String executeStatusName = EnumDicPublic.EXECUTE_FINISH.getValue();
			this.getSchemeInfoService().editExecuteTaskStatus(executeTaskId, executeStatusId, executeStatusName, personName);
			
			String recordContent = RehearseGlobal.ActionDataPool.get(executeTaskId).get("taskName")+":执行结束!";
			//记录结束执行信息
			TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
			taskExecuteRecord.setExecuteTaskId(executeTaskId);
			taskExecuteRecord.setIsWarn("0");
			taskExecuteRecord.setRecordTypeId(EnumDicPublic.INFO_END_EXECUTE.getKey());
			taskExecuteRecord.setRecordTypeName(EnumDicPublic.INFO_END_EXECUTE.getValue());
			taskExecuteRecord.setRecordContent(recordContent);
			this.getTaskExecuteRecordService().insertRecord(taskExecuteRecord, personName);
			//推送结束执行消息
			com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
			Map<String, Object> endMap = new HashMap<String, Object>();
			endMap.put("type", "end");
			endMap.put("recordTime", TimeHelper.dateToLongString());
			endMap.put("content", recordContent);
			jsonObject2.put(routeId, endMap);
			this.getServerPushServiceImpl().pushMessage("" + executeTaskId, jsonObject2);
			RehearseGlobal.ActionDataPool.remove(executeTaskId);//移除缓存信息
		}else if("change".equals(state)) {//切换线路
			String routeIdOld = "";
			if(RehearseGlobal.ActionDataPool.containsKey(executeTaskId)) {
				routeIdOld = (String)RehearseGlobal.ActionDataPool.get(executeTaskId).get("routeId");
				RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);//暂停
				if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid")!=null){//删除定位订阅
					this.getPositionService().removeListener(Long.parseLong(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listerid").toString()));
				}
				if(RehearseGlobal.ActionDataPool.get(executeTaskId).get("listenerIdPass")!=null){//删除卡口过车数据订阅
					this.getVehicleCallbackService().removeVehiclePassListener(executeTaskId);
				}
				RehearseGlobal.ActionDataPool.remove(executeTaskId);//移除缓存信息
			}
			
			RehearseGlobal.ActionDataPool.put(executeTaskId,new ConcurrentHashMap<>());
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("routeId",routeId);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("executeTaskId",executeTaskId);
			Map<String, Object> lastGeo = new HashMap<String, Object>();//上一次有效定位数据
			Map<String, Object> lastDeviateGeo = new HashMap<String, Object>();//偏离报警的上一次有效定位数据
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("lastGeo",lastGeo);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("lastDeviateGeo",lastDeviateGeo);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",false);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("positionWranNum",0);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("isDeviateCoord",false);
			//更新初始化数据
			datainit(executeTaskId,positionDeviceId);
			
			if("".equals(routeIdOld)) {//如果不存在执行线路、则默认获取主线路编号
				List<RouteInfoDto> routeInfoDtolist = (List<RouteInfoDto>) this.getDubboRouteInfoService()
						.getRouteInfoList(null, null, taskId, null, null, null).getResultList();
				routeIdOld = routeInfoDtolist.get(0).getRouteId();
			}
			//切换为主线路
			String taskId = (String)RehearseGlobal.ActionDataPool.get(executeTaskId).get("taskId");
			if(null != taskId && !"".equals(taskId)) {
				iDubboRouteInfoService.changeRouteType(taskId, routeId, personName);
			}
			String recordContent = RehearseGlobal.ActionDataPool.get(executeTaskId).get("taskName")+":已切换线路!";
			//记录结束执行信息
			TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
			taskExecuteRecord.setExecuteTaskId(executeTaskId);
			taskExecuteRecord.setIsWarn("0");
			taskExecuteRecord.setRecordTypeId(EnumDicPublic.INFO_ROUTE_CHANGE.getKey());
			taskExecuteRecord.setRecordTypeName(EnumDicPublic.INFO_ROUTE_CHANGE.getValue());
			taskExecuteRecord.setRecordContent(recordContent);
			this.getTaskExecuteRecordService().insertRecord(taskExecuteRecord, personName);
			//推送切换线路执行消息
			com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
			Map<String, Object> endMap = new HashMap<String, Object>();
			endMap.put("type", "changeRoute");
			endMap.put("recordTime", TimeHelper.dateToLongString());
			endMap.put("content", recordContent);
			endMap.put("routeId", routeId);
			jsonObject2.put(routeIdOld, endMap);
			this.getServerPushServiceImpl().pushMessage("" + executeTaskId, jsonObject2);
			
		}else if("reset".equals(state) && RehearseGlobal.ActionDataPool.containsKey(executeTaskId)) {//重置线路
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",true);//先暂停执行原线路
			Map<String, Object> lastGeo = new HashMap<String, Object>();//上一次有效定位数据
			Map<String, Object> lastDeviateGeo = new HashMap<String, Object>();//偏离报警的上一次有效定位数据
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("lastGeo",lastGeo);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("lastDeviateGeo",lastDeviateGeo);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("positionWranNum",0);
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("isDeviateCoord",false);
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).containsKey("iPoint")) {
				RehearseGlobal.ActionDataPool.get(executeTaskId).remove("iPoint");//移除途径点序号
			}
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).containsKey("pushCameraListIndex")) {
				RehearseGlobal.ActionDataPool.get(executeTaskId).remove("pushCameraListIndex");//移除视频序号
			}
			if(RehearseGlobal.ActionDataPool.get(executeTaskId).containsKey("pushXhjListIndex")) {
				RehearseGlobal.ActionDataPool.get(executeTaskId).remove("pushXhjListIndex");//移除信号机序号
			}
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("stop",false);//重新开始执行
		}
		
		return false;
	}
	@SuppressWarnings("unchecked")
	public  void datainit(String executeTaskId,String positionDeviceId){

		TaskDetailInfo taskInfo=this.getTaskInfoService().selectExecuteTaskInfoById(executeTaskId);
		
		if(positionDeviceId != null && !"".equals(positionDeviceId)){
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("positionDeviceId",positionDeviceId);
			LoggerHelper.LOG.info("任务"+taskInfo.getTaskName()+"绑定用户传入的定位设备:"+taskInfo.getPositionDeviceId());
		}else if(taskInfo.getPositionDeviceId()!=null && !"".equals(taskInfo.getPositionDeviceId())){
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("positionDeviceId",taskInfo.getPositionDeviceId());
			LoggerHelper.LOG.info("任务"+taskInfo.getTaskName()+"从数据库绑定定位设备:"+taskInfo.getPositionDeviceId());
		}
		if(!RehearseGlobal.ActionDataPool.get(executeTaskId).containsKey("positionDeviceId")){
			LoggerHelper.LOG.info("任务"+taskInfo.getTaskName()+"未绑定定位设备，无法监测到车辆轨迹。。。。。。");

		}
		List<RouteInfoDto> routeInfoDtolist = (List<RouteInfoDto>) this.getDubboRouteInfoService()
				.getRouteInfoList(RehearseGlobal.ActionDataPool.get(executeTaskId).get("routeId").toString(), null, null, null, null, null).getResultList();
		List<RouteInfo> routeInfoList=EntityChangeHelper.routeInfoDtoToPojoList(routeInfoDtolist);
		List<RoutePoint> routePointList = routeInfoList.get(0).getRoutePointList(); // 线路途径点集合
		// routePalnTime = routeInfoList.get(0).getRoutePlanTime();// 计划用时
		List<RltRouteDevice> rltRouteDevicelist = routeInfoList.get(0).getRltRouteDeviceList();// 设备
		List<RltRouteDevice> cameraList = new ArrayList<RltRouteDevice>();
		List<RltRouteDevice> xhjList = new ArrayList<RltRouteDevice>();
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("taskId" ,taskInfo.getTaskId());
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("taskName" ,taskInfo.getTaskName());
		if(taskInfo.getAdvanceReleaseDistance() != null && taskInfo.getAdvanceReleaseDistance() > 0) {
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("advanceReleaseDistance" ,taskInfo.getAdvanceReleaseDistance());
		}else {
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("advanceReleaseDistance" ,200);
		}
		String beforeLicensePlate = taskInfo.getBeforeLicensePlate();//前导车牌
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("beforeLicensePlate" ,beforeLicensePlate);
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("routeInfoList" ,routeInfoList);
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("routePointList" , routePointList);
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("rltRouteDevicelist" , rltRouteDevicelist);
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("cameraList" , cameraList);
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("xhjList" , xhjList);
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("deviceidList" , new ArrayList<>());
		RehearseGlobal.ActionDataPool.get(executeTaskId).put("xhjidList" , new ArrayList<>());
		//获取途径点类型为卡口点，并进行订阅
		List<String> bayonetIds = new ArrayList<String>();
		for (int i = 0; i < routePointList.size(); i++) {
			if (EnumDicPublic.ROUTE_BAYONET.getKey().equals(routePointList.get(i).getPointTypeId())) {
				if(null != routePointList.get(i).getRelateId() && !"".equals(routePointList.get(i).getRelateId())) {
					bayonetIds.add(routePointList.get(i).getRelateId());
				}
			}
		}
		if(bayonetIds.size() > 0) {
			String listenerIdPass = executeTaskId;
			this.getVehicleCallbackService().addVehiclePassListener(listenerIdPass, bayonetIds, beforeLicensePlate, 
					VehiclePassListenerImpl.getInstance());
			RehearseGlobal.ActionDataPool.get(executeTaskId).put("listenerIdPass",listenerIdPass);
		}
		//关联设备，区分视频、信号机
		for (int i = 0; i < rltRouteDevicelist.size(); i++) {
			if (EnumDicPublic.DEVICE_TYPE_VEDIO.getKey().equals(rltRouteDevicelist.get(i).getDeviceTypeId())) {
				cameraList.add(rltRouteDevicelist.get(i));
			}else if (EnumDicPublic.DEVICE_TYPE_SIGNAL.getKey().equals(rltRouteDevicelist.get(i).getDeviceTypeId())) {
				xhjList.add(rltRouteDevicelist.get(i));
			}

		}
		Collections.sort(routePointList, new Comparator<RoutePoint>() {
			/*
			 * int compare(Person p1, Person p2) 返回一个基本类型的整型， 返回负数表示：p1
			 * 小于p2， 返回0 表示：p1和p2相等， 返回正数表示：p1大于p2
			 */
			@Override
			public int compare(RoutePoint p1, RoutePoint p2) {
				if (p1.getPointSortno() > p2.getPointSortno()) {
					return 1;
				}
				if (p1.getPointSortno() == p2.getPointSortno()) {
					return 0;
				}
				return -1;
			}
		});
		if (cameraList != null) {
			Collections.sort(cameraList, new Comparator<RltRouteDevice>() {
				/*
				 * int compare(Person p1, Person p2) 返回一个基本类型的整型， 返回负数表示：p1
				 * 小于p2， 返回0 表示：p1和p2相等， 返回正数表示：p1大于p2
				 */
				@Override
				public int compare(RltRouteDevice p1, RltRouteDevice p2) {
					if (p1.getDeviceLength() > p2.getDeviceLength()) {
						return 1;
					}
					if (p1.getDeviceLength() == p2.getDeviceLength()) {
						return 0;
					}
					return -1;
				}

			});
		}
		if (xhjList != null) {
			Collections.sort(xhjList, new Comparator<RltRouteDevice>() {
				/*
				 * int compare(Person p1, Person p2) 返回一个基本类型的整型， 返回负数表示：p1
				 * 小于p2， 返回0 表示：p1和p2相等， 返回正数表示：p1大于p2
				 */
				@Override
				public int compare(RltRouteDevice p1, RltRouteDevice p2) {
					if (p1.getDeviceLength() > p2.getDeviceLength()) {
						return 1;
					}
					if (p1.getDeviceLength() == p2.getDeviceLength()) {
						return 0;
					}
					return -1;
				}
			});
		}
	}
	public void showfirstsigne(String executeTaskId){
		com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
		List<RltRouteDevice>  xhjList =(List<RltRouteDevice>)RehearseGlobal.ActionDataPool.get(executeTaskId).get("xhjList");
		ArrayList<String> xhjidList = new ArrayList<>();
		if (xhjList != null && xhjList.size() != 0) {
			Map<String, Object> deviceMap = new HashMap<String, Object>();
			ArrayList beforedevlist = new ArrayList<>();
			ArrayList afterdevlist = new ArrayList<>();
			xhjidList.clear();
			for (int i = 0; i < 4; i++) {
				if (i < xhjList.size()) {
					RltRouteDevice rltRouteDevice = new RltRouteDevice();
					rltRouteDevice.setDeviceId(xhjList.get(i).getDeviceId());
					rltRouteDevice.setDeviceName(xhjList.get(i).getDeviceName());
					rltRouteDevice.setDeviceTypeId(xhjList.get(i).getDeviceTypeId());
					rltRouteDevice.setManageId(xhjList.get(i).getManageId());
					rltRouteDevice.setSetBitBefore(xhjList.get(i).getSetBitBefore());
					rltRouteDevice.setSetBitAfter(xhjList.get(i).getSetBitAfter());
					xhjidList.add(xhjList.get(i).getDeviceId());
					beforedevlist.add(rltRouteDevice);
				}
			}
			deviceMap.put("type", "signal");
			deviceMap.put("beforedevice", beforedevlist);
			deviceMap.put("afterdevice", afterdevlist);
			jsonObject2.put(RehearseGlobal.ActionDataPool.get(executeTaskId).get("routeId").toString(), deviceMap);
			this.getServerPushServiceImpl().pushMessage("" + executeTaskId, jsonObject2);
		}
	}
	public void showfirstVideo(String executeTaskId){
		com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
		ArrayList<String> deviceidList = new ArrayList<>();

		List<RltRouteDevice>  cameraList =(List<RltRouteDevice>)RehearseGlobal.ActionDataPool.get(executeTaskId).get("cameraList");
		if (cameraList != null && cameraList.size() != 0) {
			Map<String, Object> deviceMap = new HashMap();
			ArrayList devlist = new ArrayList<>();
			deviceidList.clear();
			for (int i = 0; i < 2; i++) {
				if (i < cameraList.size()) {
					RltRouteDevice rltRouteDevice = new RltRouteDevice();
					rltRouteDevice.setDeviceId(cameraList.get(i).getDeviceId());
					rltRouteDevice.setDeviceName(cameraList.get(i).getDeviceName());
					rltRouteDevice.setDeviceTypeId(cameraList.get(i).getDeviceTypeId());
					rltRouteDevice.setManageId(cameraList.get(i).getManageId());
					rltRouteDevice.setSetBitBefore(cameraList.get(i).getSetBitBefore());
					rltRouteDevice.setSetBitAfter(cameraList.get(i).getSetBitAfter());
					deviceidList.add(cameraList.get(i).getDeviceId());
					devlist.add(rltRouteDevice);
				}
			}
			deviceMap.put("type", "video");
			deviceMap.put("cameradevice", devlist);
			jsonObject2.put(RehearseGlobal.ActionDataPool.get(executeTaskId).get("routeId").toString(), deviceMap);
			this.getServerPushServiceImpl().pushMessage("" + executeTaskId, jsonObject2);
		}
	}
}
